package com.yingluo.abacus.plantform.docs.app.hiberante;

import com.yingluo.abacus.boot.core.data.hibernate.utils.HibernateUtils;
import com.yingluo.abacus.plantform.docs.app.hiberante.vo.StudentVo;
import com.yingluo.abacus.plantform.docs.app.utils.DocsAppUtils;
import com.yingluo.abacus.plantform.docs.domain.app.jpa.Student;
import com.yingluo.abacus.plantform.utils.PlantformUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * http://blog.csdn.net/catoop/article/details/50575038
 * 
 * @author sunzh
 *
 */
@RestController
public class JpaController implements Serializable {
	public static final long serialVersionUID = 1618300630846652603L;

	private final static String PATH= DocsAppUtils.MOUDLE_PATH+"/hibernate/jpa";
	private static final Logger logger = LoggerFactory.getLogger(SaveController.class);

    @RequestMapping(value = PlantformUtils.PROJECT_PATH  +PATH   )
	public List<StudentVo> execute() throws Exception {
        return service.save();
	}

//	@RequestMapping(value = DocsAppUtils.MODULE_DOCSAPP +FUN_PATH )
//	public String query() throws Exception {
//		service.getList();
//
//		return null;
//	}

	@Autowired
	@Qualifier( "service_"+JpaController.serialVersionUID )
	JpaService service;

}

@Service("service_"+JpaController.serialVersionUID )
class JpaService {

	@Autowired
	private JdbcTemplate jdbcTemplate;

//	@TargetDataSource(name = "ds2")
//	@Transactional
//	public List<Student> getList() {
//		String sql = "SELECT ID  FROM STUDENT";
//		return (List<Student>) jdbcTemplate.query(sql, new RowMapper<Student>() {
//
//			@Override
//			public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
//				Student stu = new Student();
//				stu.setId(rs.getInt("ID"));
//				return stu;
//			}
//
//		});
//	}

	@Transactional
	public List<StudentVo> save() throws Exception {

		Student po = new Student();
		po.setName("sss");
		studentRepository.save(po);
		if (false)
			throw new RuntimeException("error");
		List<Student> s = studentRepository.findByName("sss");
        List<StudentVo> sr=new ArrayList<StudentVo>();
		for (Student obj:s){
            StudentVo bo= HibernateUtils.copy(StudentVo.class,obj);
            sr.add(bo);
        }
		return sr;
	}

	@Autowired
	@Qualifier("repository_"+JpaController.serialVersionUID  )
	private StudentRepository studentRepository;

}

@Repository("repository_"+JpaController.serialVersionUID)
interface StudentRepository extends CrudRepository<Student, Integer> {

	@Query(value = "select s from Student s where s.name = ?1")
	List<Student> findByName(@Param("name") String name);

}
